package src;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintStream;
import java.io.UnsupportedEncodingException;
import java.util.Arrays;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

/*
 * Created on Nov 24, 2009
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */

/**
 * @author Danish Contractor (dcontrac@in.ibm.com)
 * 
 * TODO To change the template for this generated type comment go to Window -
 * Preferences - Java - Code Style - Code Templates
 */

public class TokenFetcher {
	//public static String lexFile = "\"C:/Documents and Settings/Administrator/workspace/smsindex/data/dic/vocab.wts_lcase.txt\"";
	public static String lexFile = "\"C:/smsindex/smsindex/data/Hindi/lex.e2f\"";
	//static String lexFile="\"C:/Documents and Settings/Administrator/My
	// Documents/vocab.txt\"";
	static String tempFile1 = "C:/query1.txt";

	static String tempFile2 = "C:/query2.txt";

	int first;

	int second;

	static String sortCmdPath = "C:/cygwin/bin/sort.exe";

	static Map transTable = null;

	//static String tmpFilePath="C:/Documents and Settings/Administrator/My
	// Documents/LVS/LEX FILES/1/TEMP.txt";
		/**
     * Returns sorted List of n-tokens.
     * @param List of n-Tokens
     * @throws None
     */
	public static List sortTokenList(List l1) {
		Object[] arr = l1.toArray();
		Comparator c1 = Token.DecreasingOrderOfScores();
		Arrays.sort(arr, c1);
		return Arrays.asList(arr);

	}
	/**
     * Loads Map from lexFile. This Map contains every word in the vocab along with its n-best translations sorted in decreasing order of probability
     * scores.
     * @param String lexFile
     * @throws Exception 
     * 
     */
	public static void LoadMap(String lexFile) throws Exception,
			FileNotFoundException {
		long start = System.currentTimeMillis();
		transTable = new HashMap();
		String word = "";
		List l1;
		BufferedReader b1 = new BufferedReader(new InputStreamReader(
				new FileInputStream(lexFile), "UTF8"));
		String read = "";
		String[] tuple;
		List temp = null;
		while ((read = b1.readLine()) != null) {
			//PrintStream sysout = new PrintStream(System.out, true, "UTF-8");
			//sysout.println("Reading:" + read);
			tuple = read.split(" ");
			//if(tuple[1].equalsIgnoreCase(word))
			if(tuple.length > 2){
				if (transTable.containsKey((String) tuple[1])) {
					temp = (LinkedList) transTable.get((String) tuple[1]);
					if (!temp.contains(tuple[0]) && !tuple[0].equals("NULL")) {
						temp.add(new Token(tuple[0], new Float(tuple[2])
						.floatValue()));
						transTable.put(tuple[1], temp);
					}
					//l1.add(new Token(tuple[0],new Float(tuple[2]).floatValue()));
				} else {
					temp = new LinkedList();
					temp.add(new Token(tuple[0], new Float(tuple[2]).floatValue()));
					transTable.put(tuple[1], temp);
				}
			}
		}
		long end = System.currentTimeMillis();
		System.out.println("MAP LOADED INTO MEMORY: " + transTable.size()
				+ " entries in " + (end - start) + " milliseconds");
		b1.close();
		start = System.currentTimeMillis();
		System.out.print("Sorting Map Lists by scores...");
		Iterator i1 = transTable.entrySet().iterator();
		while (i1.hasNext()) {
			Map.Entry pairs = (Map.Entry) i1.next();
			temp = (LinkedList) pairs.getValue();
			temp = sortTokenList(temp);
			transTable.put((String) pairs.getKey(), temp);
		}
		end = System.currentTimeMillis();
		System.out.println("Done.");
		System.out.println("Sorting took : " + (end - start) + " milliseconds");

	}
	/**
     * Dumps the Map to an output text file 
     * @param String outputFile
     * @throws Exception 
     * 
     */
	public static void dumpMap(String outputFile) throws Exception,
			FileNotFoundException {
		if (transTable == null)
			return;
		BufferedWriter b2 = new BufferedWriter(new OutputStreamWriter(
				new FileOutputStream(outputFile), "UTF-8"));
		Iterator i1 = transTable.entrySet().iterator();
		Iterator i2 = null;
		Token temp;
		List tempList;
		while (i1.hasNext()) {
			Map.Entry pairs = (Map.Entry) i1.next();
			tempList = (List) pairs.getValue();
			b2.write((String) pairs.getKey());

			i2 = tempList.iterator();
			while (i2.hasNext()) {
				b2.write("\t\t" + ((Token) i2.next()).toString());
				b2.write("\n");
				b2.flush();
			}

		}
		b2.close();
	}

		public static void getVocab(){
		try {
			String lexfile1 = "C:/Documents and Settings/Administrator/workspace/smsindex/data/dic/check";
			BufferedReader b1  = new BufferedReader(new InputStreamReader (new FileInputStream (lexfile1), "UTF8"));
			String vocabfile = "C:/Documents and Settings/Administrator/workspace/smsindex/data/dic/temp";
		 	BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(vocabfile),"UTF8"));
			String read = "";
			while((read=b1.readLine())!=null)
			{
				System.out.println(read);
				String [] tuple = read.split(" ");
				System.out.println(tuple.length);
				if(tuple.length > 2){
					writer.write(tuple[1]);
					writer.newLine();
				}
				//System.out.println(tuple[1]);				
			}
		 	writer.close();
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	/**
     * Returns the translation table map
     * @return Map
     * @throws None
     */
	public static Map returnMap() {
		return transTable;
	}
	/**
     * Returns the n-best tokens for given word using the Map.
     * @return List
     * @param String word
	 * @throws Exception
     */
	
	public static List<Token> getTokenList2(String word) throws Exception {
		if(transTable==null)
		{
			throw new Exception(("THE TRANSLATION TABLE IS NOT INIT. PLEASE RUN LOADMAP()"));
		}
		if (transTable.containsKey(word)) {
			return (List) transTable.get(word);
		} else
			return null;
	}
	/**
     * Returns the n-best tokens for given word using the lex file.
     * @return List
     * @param String word
	 * @throws Exception
     */

	public static List getTokenList(String word) throws Exception {
		List l1 = new LinkedList();
		//String query="grep \' "+word+" \' "+lexFile+" | "+sortCmdPath+" -nr 3
		// | head > " + tmpFilePath;
		String query1 = "grep \' " + word + " \' " + lexFile;
		BufferedWriter b2 = new BufferedWriter(new OutputStreamWriter(
				new FileOutputStream(tempFile1), "UTF-8"));
		Process p = Runtime.getRuntime().exec(query1);
		String s = "";
		BufferedReader stdInput = new BufferedReader(new InputStreamReader(p
				.getInputStream(), "UTF-8"));

		while ((s = stdInput.readLine()) != null) {
			b2.write(s);
			b2.write("\n");
			b2.flush();
		}
		b2.close();
		stdInput.close();
		String query2 = sortCmdPath + " -nrk 3 " + tempFile1;
		b2 = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(
				tempFile2), "UTF-8"));
		p = Runtime.getRuntime().exec(query2);
		stdInput = new BufferedReader(new InputStreamReader(p.getInputStream(),
				"UTF-8"));
		while ((s = stdInput.readLine()) != null) {
			b2.write(s);
			b2.write("\n");
			b2.flush();
		}
		b2.close();
		stdInput.close();
		/*
		 * BufferedReader stdError = new BufferedReader(new
		 * InputStreamReader(p.getErrorStream()));
		 *  // read any errors from the attempted command
		 * System.out.println("STANDARD ERROR"); while ((s =
		 * stdError.readLine()) != null) { System.out.println(s); }
		 *  
		 */
		BufferedReader b1 = new BufferedReader(new InputStreamReader(
				new FileInputStream(tempFile2), "UTF8"));
		String read = "";
		String[] tuple;
		while ((read = b1.readLine()) != null) {
			tuple = read.split(" ");
			if (tuple[1].equalsIgnoreCase(word)) {
				l1.add(new Token(tuple[0], new Float(tuple[2]).floatValue()));
			}
		}
		return l1;
	}

	public static void main(String[] args) throws Exception {
		PrintStream sysout = new PrintStream(System.out, true, "UTF-8");
		//LoadMap(lexFile.replace("\"", ""));// remove quotes from constant.
		//---------------------------------------
		long start = System.currentTimeMillis();
		/*List results = getTokenList2("how");
		long end = System.currentTimeMillis();
		System.out.println("Look up took: " + (end - start)
				+ " milliseconds (Using new Map)");
		Iterator i1 = results.iterator();
		Token temp;
		while (i1.hasNext()) {
			temp = (Token) i1.next();
			System.out.println(temp.token + " " + temp.probabilty);
		}
		start = System.currentTimeMillis();*/
		List results = getTokenList("medical");
		long end = System.currentTimeMillis();
		sysout.println("-------------------");
		sysout.println("Look up took: " + (end - start)
				+ " milliseconds (Using file)");
		Iterator i1 = results.iterator();
		while (i1.hasNext()) {
			Token temp = (Token) i1.next();
			sysout.println(temp.token + " " + temp.probability);
		}
		//----------------------
		// dumpMap("C:/DUMP.txt"); // DUMPS output of Map.

	}
}